package com.hongminzheng.openapi.controller;

import com.github.pagehelper.PageInfo;
import com.hongminzheng.openapi.bean.Result;
import com.hongminzheng.openapi.bean.TableData;
import com.hongminzheng.openapi.pojo.Route;
import com.hongminzheng.openapi.service.RouteService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hmz
 * @version v1.0
 * @data 2020/09/29
 **/
@RestController
@RequestMapping("/route")
public class RouteController {
    /**
     * 创建服务层对象
     */
    @Autowired
    private RouteService routeService;

    /**
     * 增加
     * @param route
     * @return
     */
    @PostMapping("/add")
    public Result insertRoute(Route route){
        try {
            routeService.insert(route);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            return new Result(false, "添加失败");
        }
    }
    /**
     * 更新
     * @param route
     * @return
     */
    @RequestMapping("/update")
    public Result updateRoute(Route route){
        try {
            routeService.updateByPrimaryKey(route);
            return new Result(true, "更新成功");
        } catch (Exception e) {
            return new Result(false, "更新失败");
        }
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return routeTableData
     */
    @GetMapping("/page")
    public TableData<Route> findLayuiPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,String gatewayApiName, Integer state) {
        Route route = new Route();
        route.setGatewayApiName(gatewayApiName);
        route.setState(state);

        TableData<Route> routeTableData = new TableData<>();
        PageInfo<Route> routePageInfo = routeService.findPage(page, limit,route);

        routeTableData.setCount(routePageInfo.getTotal());
        routeTableData.setData(routePageInfo.getList());
        return routeTableData;
    }

    @PostMapping("/dels")
    public Result dels(@RequestBody List<Route> routeList) {
        //创建返回对象
        Result result = new Result();
        //创建循环，调用删除方法
        routeList.forEach(route -> {
            routeService.deleteByPrimaryKey(route.getId());
        });
        result.setStatus(true);
        result.setMessage("批量删除成功");
        return result;
    }


}
